#include "FileIO.h"
#include "Error.h"
#include "rapidxml.hpp"
#include "AssetManager.h"
#include <fstream>
#include <vector>

namespace Engine
{
	XMLDocSPtr LoadXMLFile(const std::string& name, StringSPtr fileStr)
	{
		std::ifstream xmlFile(name);

		if(xmlFile.fail()) 
		{
			Error("Unable to open xml file " + name);
			return XMLDocSPtr();
		}

		std::string line;
		while(std::getline(xmlFile, line))
			fileStr ->append(line + "\n");

		fileStr ->append("\0");

		xmlFile.close();

		XMLDocSPtr doc(new rapidxml::xml_document <> ());

		try
		{
			doc ->parse <0> (&fileStr ->at(0));
		}
		catch(std::exception e)
		{
			std::string error("Error while parsing xml file ");
			error += name;
			error += ": ";
			error += e.what();

			doc.reset();
			Error(error);
		}
		return doc;
	}

	StringSPtr LoadFileString(const std::string& name)
	{
		std::ifstream file(name);

		if(file.fail()) 
		{
			Error("Unable to open file " + name);
			return StringSPtr();
		}
		std::string line;

		StringSPtr string(new std::string);
		while(std::getline(file, line))
			string ->append(line + "\n");

		return string;
	}
}
			